﻿using System.Net.Mail;
using System.Text;

namespace Netkk.Common
{
    /// <summary>
    /// Summary description for Sendmail
    /// </summary>
    public class Sendmail
    {
        private readonly MailAddress sender;
        private MailMessage msg;
        private readonly SmtpClient server;

        public bool IsHtml { get; set; }
        public MailAddressCollection Bcc { get; private set; }
        public MailAddressCollection Cc { get; private set; }

        public Sendmail(MailAddress mailSender)
        {
            Bcc = new MailAddressCollection();
            Cc = new MailAddressCollection();
            server = new SmtpClient();
            sender = mailSender;
        }


        public void Send(MailAddress emailAddress, string subject, string message)
        {
            Send(new MailAddressCollection { emailAddress }, subject, message, sender);
        }
        public void Send(MailAddressCollection emailAddresses, string subject, string message)
        {
            Send(emailAddresses, subject, message, sender);
        }
        public void Send(MailAddress emailAddress, string subject, string message, MailAddress replyTo)
        {
            Send(new MailAddressCollection { emailAddress }, subject, message, sender);
        }
        public void Send(MailAddressCollection emailAddresses, string subject, string message, MailAddress replyTo)
        {
            msg = new MailMessage
            {
                ReplyTo = replyTo,
                Sender = sender,
                From = sender,
                Subject = subject,
                Body = message,
                BodyEncoding = Encoding.Default,
                IsBodyHtml = IsHtml
            };
            foreach (MailAddress mailAddress in emailAddresses)
            {
                msg.To.Add(mailAddress);
            }
            foreach (MailAddress mailAddress in Cc)
            {
                msg.CC.Add(mailAddress);
            }
            foreach (MailAddress mailAddress in Bcc)
            {
                msg.Bcc.Add(mailAddress);
            }

            server.Send(msg);
        }
    }
}